package com.ghck.shop.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ghck.shop.model.request.ShoppingCartReq;
import com.ghck.shop.model.response.ShoppingCartRes;

public interface ShoppingCartMapper {

	@Insert("insert into shopping_cart(user_id,goods_id,specification_id,goods_num) "
			+ " values(#{userId},#{goodsId},#{specificationId},#{goodsNum})")
	@Options(useGeneratedKeys=true)
	public Integer insert(ShoppingCartReq shoppingCartReq);
	
	@Delete("delete from shopping_cart where id = #{id}")
	public Integer delete(Integer id);
	
	@Update("update shopping_cart set goods_num = case when (goods_num + #{goodsNum}) <= 1 then 1 else goods_num + #{goodsNum} end "
			+ " where user_id = #{userId} and goods_id = #{goodsId} and specification_id=#{specificationId}")
	public Integer updateNum(ShoppingCartReq shoppingCartReq);
	
	
	@Update("update shopping_cart set goods_num = case when (goods_num + #{goodsNum}) <= 1 then 1 else #{goodsNum} end "
			+ " where user_id = #{userId} and goods_id = #{goodsId}  and specification_id=#{specificationId}")
	public Integer update(ShoppingCartReq shoppingCartReq);
	
	@Select("select sum(goods_num) cnt from shopping_cart where user_id = #{userId}")
	public Integer findShoppingCartNum(ShoppingCartReq shoppingCartReq);
	
	
	@Select("select a.id,a.goods_id,a.specification_id,a.goods_num,b.title,b.amount,b.subject,b.thumbnail_image,b.goods_type,b.sale_price,"
			+ "b.market_price from shopping_cart a left join goods b on a.goods_id = b.id where user_id = #{userId}"
			+ " order by b.create_time asc limit #{start},#{limit}")
	public List<ShoppingCartRes> findShoppingCartList(ShoppingCartReq shoppingCartReq);
	
	@Select("select * from shopping_cart where user_id = #{userId} and goods_id = #{goodsId} and specification_id=#{specificationId}")
	public ShoppingCartReq findShoppingCartByGoodsIdAndUserId(ShoppingCartReq shoppingCartReq);
	
	@Select("select * from shopping_cart where id = #{id}")
	public ShoppingCartReq findShoppingCartById(ShoppingCartReq shoppingCartReq);
}
